package com.Smart.smartscals.DataBase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.Smart.smartscals.Entity.ReciveBodyInfoEntity;


import android.content.Context;
import android.database.Cursor;



public class FileHelpService {
	
	private String absoluteExPath;
	private String absoluteImPath;
    public static final String BASEPATH = "data/data/com.example.databasedemo";
    
    private String date;
    private String weight;
    private String fat;
    private String result;
    private String state;
    private String water_capacity;
    private String defecate_times;
    private String defecate_color;
    private String defecate_state;
    private String diet_times;
    private String sock;
    private String sleep;
    private String temp2ex;
    private String imdate;
    private String imweight;
    private String imfat;
    private String imresult;
    private String imstate;
    private String imwater_capacity;
    private String imdefecate_times;
    private String imdefecate_color;
    private String imdefecate_state;
    private String imdiet_times;
    private String imsock;
    private String imsleep;   
 
    private String temp2in;
    private SharedParam sp;
    
    public FileHelpService(Context context){    	
  	
    	createFilePath();
    	sp = SharedParam.getInstance(context);
    }
    
    public void createFilePath(){  
		//if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			File tfile = new File(BASEPATH);
        	if(!tfile.exists()){
        		tfile.mkdirs();
        	} 
		//}
    }


    public boolean writeFileData(String userName,long fromDate,long toDate ,DataBaseService dataservice,Context context){ 
    	createFilePath();
    	SeletDataBasaUtils selectDataBaseUtils = new SeletDataBasaUtils(context);
    	ArrayList<ReciveBodyInfoEntity> list = selectDataBaseUtils.getBodyInfoEntity(fromDate,toDate,userName);
//    	Cursor cur = dataservice.seletBodyInfo(fromDate, toDate,userName);
//    	String userName =dataservice.getUserNameByUid(uid);
    	
    	absoluteExPath = BASEPATH+"/"+userName.replace(" ", "")+":"+TimeDateUtils.convert2FileName(fromDate)+
    			"-"+TimeDateUtils.convert2FileName(toDate)+".txt";  
//    	absoluteExPath = BASEPATH+"/test.txt";
    	
        try{ 
         FileOutputStream fout =new FileOutputStream(absoluteExPath);
         String  currentUserId = sp.getCurrentUserId();
         ReciveBodyInfoEntity recvBodyInfoEntity = new ReciveBodyInfoEntity();
         for (int i = 0; i < list.size(); i++) {
			fout.write(currentUserId.getBytes());
			fout.write(",".getBytes());
			recvBodyInfoEntity = list.get(i);
			long insertData = recvBodyInfoEntity.getDate();
			String userFat = recvBodyInfoEntity.getBodyFat()+"";
			String userWeight = recvBodyInfoEntity.getWeight()+"";
			String dataStr = TimeDateUtils.convert2FileDate(insertData);
			String bodyInfoStr = dataStr+","+"0"+","+"0"+","+userFat+","+"0"+
			","+"0"+","+"0"+","+"0"+","+"0"+","+userWeight+","+"0"+","+"0";
        	 fout.write(bodyInfoStr.getBytes());
        	 
        	   fout.write("\n".getBytes());
		}
         
         //启动一个线程进行网络访问，上传数据.
         
//         fout.write("This is for the prefix".getBytes());
//         fout.write("\n".getBytes());       
//         fout.write("you can define anything".getBytes());
//         fout.write("\n".getBytes());
//         while(cur.moveToNext()){        	 
//    	    date = cur.getLong(cur.getColumnIndex("date"))+"";
//    	    weight = cur.getFloat(cur.getColumnIndex("weight"))+"";
//    	    fat = cur.getFloat(cur.getColumnIndex("fat"))+"";
//    	    result = cur.getString(cur.getColumnIndex("result"))+"";
//    	    state = cur.getString(cur.getColumnIndex("state"))+"";
//    	    water_capacity = cur.getInt(cur.getColumnIndex("water_capacity"))+"";
//    	    defecate_times = cur.getInt(cur.getColumnIndex("defecate_times"))+"";
//    	    defecate_color = cur.getInt(cur.getColumnIndex("defecate_color"))+"";
//    	    defecate_state = cur.getInt(cur.getColumnIndex("defecate_state"))+"";    	    
//    	    diet_times = cur.getInt(cur.getColumnIndex("diet_times"))+"";
//    	    sock = cur.getInt(cur.getColumnIndex("sock"))+"";
//    	    sleep = cur.getInt(cur.getColumnIndex("sleep"))+"";   	    
//    	    temp2ex = ","+date+","+weight+","+fat+","+result+","+state+","+water_capacity+","+defecate_times
//    	    		+","+defecate_color+","+defecate_state+","+diet_times+","+sock+","+sleep;
//        	fout.write(temp2ex.getBytes());
//        	fout.write("\n".getBytes());    	   
//         }
       

         fout.close(); 
//         cur.close();
         } 
        catch(Exception e){ 
         e.printStackTrace(); 
         return false;
        }
        return true;
    }
    

    
    public String getAbsoluteExPath(){
    	return absoluteExPath;
    }
    
    public boolean readFileData(String filename,long uid,DataBaseService dataservice){
		

//    	absoluteImPath  = BASEPATH+"/"+filename;
//    	String userName = dataservice.getUserNameByUid(uid);
//    	if(!filename.contains(userName.replace(" ", ""))){
//    		return false;
//    	}
//    	try {
//			FileReader fr = new FileReader(absoluteImPath);
//			BufferedReader br = new BufferedReader(fr);
//			temp2in = br.readLine();
//			temp2in = br.readLine();
//			while(br.read()!= -1){				
//				temp2in = br.readLine();
//				String[] buftemp=temp2in.split(",");
//				if(buftemp[0].length()!=0){	
//					imdate = buftemp[0];
//					imweight = buftemp[1];
//					imfat = buftemp[2];
//					imresult = buftemp[3];
//					imstate = buftemp[4];
//					imwater_capacity = buftemp[5];
//				    imdefecate_times = buftemp[6];
//				    imdefecate_color = buftemp[7];
//				    imdefecate_state = buftemp[8];
//				    imdiet_times = buftemp[9];
//				    imsock = buftemp[10];
//				    imsleep = buftemp[11];
//
//					dataservice.insertBodyFatTable(uid, Long.valueOf(imdate), Float.valueOf(imweight), 
//							Float.valueOf(imfat), imresult,imstate,	Integer.valueOf(imwater_capacity),
//							Integer.valueOf(imdefecate_times), Integer.valueOf(imdefecate_color),
//							Integer.valueOf(imdefecate_state), Integer.valueOf(imdiet_times),
//							Integer.valueOf(imsock), Integer.valueOf(imsleep));				
//				}
//			}
//
//			br.close();
//			fr.close();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}  	
    	return true;
    }
         
    
   
}
